﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Warframe
{
    public partial class Form1 : Form
    {
        private WeaponSystem currentSystem;
        private ModForm modForm;

        public Form1()
        {
            InitializeComponent();
            currentSystem = new WeaponSystem();
        }

        private void modbtn_Click(object sender, EventArgs e)
        {
            Button b = (Button) sender;
            int tag = Convert.ToInt32(b.Tag);
            if (!currentSystem.isEmpty(tag))
            {
                currentSystem.returnModToList(tag);
                currentSystem.unSetMod(tag);
            }
            b.Text = "";
            makeModForm(b);
        }

        private void makeModForm(Button button)
        {
            modForm = new ModForm();
            FlowLayoutPanel panel = new FlowLayoutPanel() { Dock=DockStyle.Fill };
            var modList = currentSystem.getAvailableMods();
            foreach(WeaponMod mod in modList)
            {
                ModButton b = new ModButton() { Text = mod.Name, Size = new Size(123, 123), Mod = mod, source = button};
                b.Click += new EventHandler(b_Click);
                panel.Controls.Add(b);
            }
            modForm.Controls.Add(panel);
            modForm.Show(this);
        }

        void b_Click(object sender, EventArgs e)
        {
            ModButton b = (ModButton)sender;
            int index = Convert.ToInt32(b.source.Tag);
            currentSystem.removeModFromList(b.Mod);
            currentSystem.setMod(b.Mod, index);
            b.source.Text = b.Mod.Name;
            modForm.Close();
        }

        private void Form1_Activated(object sender, EventArgs e)
        {
            IList<Result> results = currentSystem.getCalculations();
            dataGridView1.DataSource = results.OrderByDescending(x => x.DPS).ToList();
            dataGridView1.Refresh();
        }

        private void LevelOfEnemy_ValueChanged(object sender, EventArgs e)
        {
            currentSystem.EnemyLevel = Convert.ToInt32(LevelOfEnemy.Value);
            IList<Result> results = currentSystem.getCalculations();
            dataGridView1.DataSource = results.OrderByDescending(x => x.DPS).ToList();
            dataGridView1.Refresh();
        }
    }
}
